/*******************************************************************
 *
 * Copyright (C), 2022-2027, YouGan, All rights reserved.
 *
 * Author      : zhaoyang
 * Create Time : 2022-10-13
 * Version     : v0.1
 * Description : 
 *******************************************************************/

/*----------------------------Head file----------------------------*/
#include "hr_task.h"
#include "FreeRTOS.h"
#include "queue.h"
#include "gh3x2x_drv.h"
#include "nrf_delay.h"
#include "nrf_gpio.h"
#include "board_cfg.h"

#include "app_gs_lis3dh.h"

/*----------------------------macro file---------------------------*/


#define HR_EVT_LOG_EN		1

#if HR_EVT_LOG_EN
#include "nrf_log.h"
#define HR_EVT_LOG		NRF_LOG_INFO
#else
#define HR_EVT_LOG(...)
#endif

/*----------------------------type define--------------------------*/

/*----------------------------var define---------------------------*/
void *hr_task_handle;   	//!< HR Task handle

static QueueHandle_t Queue_hr_msg = NULL;

/*-------------------------func declaration------------------------*/

/*-----------------------------------------------------------------*/

//*****************************************************************************
// * 函 数 名  : hr_main_task
// * 函数功能  : 心率任务
// * 输入参数  :	 无
// * 输出参数  : 无
// * 返 回 值  : 无
//*****************************************************************************/
void hr_main_task(void* arg)
{
    UNUSED_PARAMETER(arg);
    
    uint8_t hr_evt = 0;

    uint32_t rec = 0;

    //关闭心率和3轴供电
    nrf_gpio_cfg_output(GH3026_PWR_PIN);
    nrf_gpio_pin_clear(GH3026_PWR_PIN);
    nrf_delay_ms(100);
    
    Queue_hr_msg = xQueueCreate(5, sizeof(uint8_t));
    
#if NRF_MODULE_ENABLED(BLE_GDX)
#else
    hr_task_msg_send(HR_EVT_START_MEASURE);

#endif
    while (1)
    {
        rec = xQueueReceive(Queue_hr_msg, &hr_evt, 100/portTICK_PERIOD_MS);
        if (pdPASS == rec)
        {
//            NRF_LOG_INFO("hr_evt = %d", hr_evt);
            switch(hr_evt)
            {
                case HR_EVT_ACC_COLLECT: 
                {
//                    NRF_LOG_INFO("HR_EVT_ACC_COLLECT");
                    //采集3轴数据
                    app_gs_acc_collect();
                }
                break;

                case HR_EVT_INT: 
                {
//                    NRF_LOG_INFO("Gh3x2xDemoInterruptProcess");
                    Gh3x2xDemoInterruptProcess();
                    
                }
                break;
        
                case HR_EVT_START_MEASURE:
                {
                    //开启三轴和心率供电
                    nrf_gpio_pin_set(GH3026_PWR_PIN);
                    app_gsensor_init();

                    HR_EVT_LOG("HR_EVT_START_MEASURE \r\n");
                    Gh3x2xDemoInit();

                    nrf_delay_ms(10);
                    Gh3x2xDemoStartSampling(GH3X2X_FUNCTION_ADT);

#if 0                        
                    uint8_t mcu_start_mode = RUN_MODE_ADT_HB_DET;
        //            goodix_app_start_app_mode = false;
                    gh30x_module_start((EMGh30xRunMode)mcu_start_mode);
#endif        
                }
                break;
        
                case HR_EVT_STOP_MEASURE:
                {
                    HR_EVT_LOG("HR_EVT_STOP_MEASURE \r\n");
                    //先停止心率
//                    Gh3x2xDemoStartSampling(GH3X2X_FUNCTION_ADT);
                    Gh3x2xDemoStopSampling(0xFFFFFFFF);
                    nrf_delay_ms(10);
//                    Gh3x2xDemoDeInit();
//                    app_gsensor_deinit();
                    
                    nrf_delay_ms(10);

                    //关闭供电
                    nrf_gpio_pin_clear(GH3026_PWR_PIN);
                    //设置IO口
                }
                break;

                case HR_EVT_SLEEP:
                {
                    HR_EVT_LOG("HR_EVT_SLEEP \r\n");
                    Gh3x2xDemoDeInit();
                    app_gsensor_deinit();
                    
                    nrf_delay_ms(10);

                    //关闭三轴和心率供电
                    nrf_gpio_pin_set(GH3026_PWR_PIN);


                    //卸载
                    nrf_gpio_cfg_input(SPI_GH3026_SS_PIN, NRF_GPIO_PIN_PULLDOWN);
                    nrf_gpio_cfg_input(SPI_GH3026_SCK_PIN, NRF_GPIO_PIN_PULLDOWN);
                    nrf_gpio_cfg_input(SPI_GH3026_MISO_PIN, NRF_GPIO_PIN_PULLDOWN);
                    nrf_gpio_cfg_input(SPI_GH3026_MOSI_PIN, NRF_GPIO_PIN_PULLDOWN);
                    nrf_gpio_cfg_input(GH3026_INT_PIN, NRF_GPIO_PIN_PULLDOWN);
                    nrf_gpio_cfg_input(GH3026_RST_PIN, NRF_GPIO_PIN_PULLUP);

//                    nrf_gpio_cfg_input(I2C_GSENSOR_PIN_SLC, NRF_GPIO_PIN_PULLUP);
//                    nrf_gpio_cfg_input(I2C_GSENSOR_PIN_SDA, NRF_GPIO_PIN_PULLUP);
                    nrf_gpio_cfg_input(I2C_GSENSOR_PIN_INT, NRF_GPIO_PIN_PULLDOWN);

                    HR_EVT_LOG("HR_EVT_SLEEP end\r\n");
                }
                break;
#if 0
                case HR_EVT_BLE_CMD_PARSE:
                {
                    extern void ble_module_recv_data_via_gdcs(uint8_t *pData, uint8_t len);
                    gdx_msg_t *ptMsg =(gdx_msg_t*) msg.u.buf;
                    HR_EVT_LOG("receive msg length:%d,buf:%x",ptMsg->len,ptMsg->buf);
                            ble_module_recv_data_via_gdcs(ptMsg->buf, ptMsg->len);
                    free(ptMsg);
                }
                break;
#endif
                break;
                default:
                break;
            }
        }
        
        hr_evt = 0;
    }
}

//*****************************************************************************
// * 函 数 名  : hr_task_msg_send
// * 函数功能  : 向心率任务发消息
// * 输入参数  :	 无
// * 输出参数  : 无
// * 返 回 值  : 无
//*****************************************************************************/
void hr_task_msg_send(uint8_t subtype)
{
    uint8_t Message = subtype;
    
    //发出消息
    if (NULL != Queue_hr_msg)
    {
        xQueueSendFromISR(Queue_hr_msg, &Message, 0);
    }
}

